<template>
  <div class="login_wrap">
    <h3 class="title">吉安市官网诉求提交平台</h3>
    <div class="login_form">
      <div class="form_title">
        <span
          v-for="(item, index) in ['账号登录', '短信登录']"
          :class="['title_count', current == index ? 'active_login' : '']"
          :key="item + index"
          @click="handleTabs(index)"
          >{{ item }}</span
        >
      </div>
      <div class="form_content" v-if="current == 0">
        <el-form :model="loginForm" ref="ruleForm">
          <el-form-item class="item">
            <el-input
              v-model.trim="loginForm.userName"
              clearable
              placeholder="请输入用户名/手机号"
              prefix-icon="el-icon-user"
            ></el-input>
          </el-form-item>
          <el-form-item class="item_pass">
            <el-input
              v-model.trim="loginForm.password"
              show-password
              clearable
              placeholder="请输入密码"
              prefix-icon="el-icon-unlock"
            ></el-input>
          </el-form-item>
          <el-form-item class="item_code">
            <div class="item_captvalue">
              <el-input
                v-model.trim="loginForm.captchaValue"
                clearable
                placeholder="请输入验证码"
              ></el-input>
              <img
                :src="imgValue"
                alt=""
                class="item_img"
                @click="fnGetImgCode()"
              />
            </div>
          </el-form-item>
          <el-form-item class="item_btn">
            <el-button
              type="primary"
              @click="fnLogin('pass')"
              :disabled="flagBtnpass"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
        <div class="form_foot">
          <a @click="clickRegiser">立即注册</a>
          <a @click="clickForget">忘记密码</a>
        </div>
      </div>
      <div class="form_content" v-if="current == 1">
        <el-form :model="loginForm" ref="ruleForm">
          <el-form-item class="item">
            <el-input
              v-model.trim="loginForm.phone"
              clearable
              placeholder="请输入手机号"
              prefix-icon="el-icon-user"
            ></el-input>
          </el-form-item>
          <el-form-item class="item_code">
            <div class="item_captvalue">
              <el-input
                v-model.trim="loginForm.captchaValue"
                clearable
                placeholder="请输入验证码"
              ></el-input>
              <img
                :src="imgValue"
                alt=""
                class="item_img"
                @click="fnGetImgCode()"
              />
            </div>
          </el-form-item>
          <el-form-item class="item_pass">
            <div class="item_verification">
              <el-input
                v-model.trim="loginForm.smsCode"
                clearable
                placeholder="请输入手机验证码"
                class="ver_input"
              ></el-input>
              <el-button
                class="ver_btn"
                @click="getVerification"
                :disabled="disabledbtn"
                >{{ text }}</el-button
              >
            </div>
          </el-form-item>
          <el-form-item class="item_btn">
            <el-button
              type="primary"
              @click="fnLogin('note')"
              :disabled="flagBtnnote"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
        <div class="form_foot">
          <a @click="clickRegiser">立即注册</a>
          <a @click="clickForget">忘记密码</a>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { loginIn, getImgCode, getsendSms, loginByPhone } from "@/api/user";

export default {
  data() {
    return {
      loginForm: {
        userName: "",
        password: "",
        phone: "",
        smsCode: "", //短信验证码
        captchaValue: "", //图形验证码
        captchaKey: "" //图形验证码key
      },
      imgValue: "", //图形验证码
      current: 0,
      timer: 60,
      text: "获取验证码",
      disabledbtn: false,
      clock: "", //定时器
      flagBtnpass: false,
      flagBtnnote: false
    };
  },
  mounted() {
    this.fnGetImgCode();
  },
  methods: {
    //登录/注册
    async fnLogin(type) {
      if (type == "pass") {
        if (this.loginForm.userName == "" || this.loginForm.password == "") {
          return this.$message.warning("用户名和密码不能为空");
        }
        this.flagBtnpass = true;
        let res = await loginIn(this.loginForm);
        if (res && res.code == 0) {
          this.flagBtnpass = false;
          window.sessionStorage.setItem("token", res.data.token);
          this.$router.push({
            path: "/"
          });
        } else {
          this.flagBtnpass = false;
          this.$message.error(res.msg);
        }
      } else if (type == "note") {
        if (this.loginForm.phone == '' || this.loginForm.smsCode == '') {
           return  this.$message.error("手机号码或手机号验证码不能为空");
          }
          this.flagBtnnote = true;
          let res = await loginByPhone({
            phone: this.loginForm.phone,
            smsCode: this.loginForm.smsCode
          });

          if (res && res.code == 0) {
            this.flagBtnnote = false;
            window.sessionStorage.setItem("token", res.data.token);
            this.$router.push({
              path: "/"
            });
          } else {
            this.flagBtnnote = false;
            this.$message.error(res.msg);
          }
        } 
      
    },

    //获取图形验证码
    async fnGetImgCode() {
      let res = await getImgCode();
      console.log(res);
      if (res && res.code == 0) {
        this.imgValue = res.data.captchaImg;
        this.loginForm.captchaKey = res.data.captchaKey;
      }
    },

    //tabs切换
    handleTabs(index) {
      this.current = index;
    },
    //获取短信验证码
    async getVerification() {
      if (
        this.loginForm.captchaValue &&
        this.loginForm.captchaKey &&
        this.loginForm.phone
      ) {
        let res = await getsendSms({
          captchaKey: this.loginForm.captchaKey,
          captchaValue: this.loginForm.captchaValue,
          phone: this.loginForm.phone
        });

        if (res.code == 0) {
          this.$message.success("短信已发送");
        }
        this.disabledbtn = true;
        this.text = this.timer + "s重新发送";
        this.clock = window.setInterval(() => {
          this.timer--;
          this.text = this.timer + "s重新发送";
          if (this.timer < 0) {
            window.clearInterval(this.clock);
            this.text = "获取验证码";
            this.timer = 60;
            this.disabledbtn = false;
          }
        }, 1000);
      } else {
        this.$message.error("请输入验证码或手机号");
      }
    },
    //到注册页
    clickRegiser() {
      this.$router.push({
        path: "/register"
      });
    },
    //找回密码
    clickForget() {
      this.$router.push({
        path: "/forgetpassword"
      });
    },
    // 清除定时器
    beforeDestroy() {
      clearInterval(this.clock);
      this.timer = this.clock;
    }
  },

  head() {
    return {
      title: "12345概况-吉安12345政府服务热线",
      meta: [
        {
          hid: "吉安12345政府服务热线",
          name: "吉安12345政府服务热线",
          content: "吉安12345政府服务热线"
        }
      ]
    };
  }
};
</script>

<style scope lang="scss">
.active_login {
  border-bottom: 2px solid #409eff;
}
.login_wrap {
  width: 100%;
  position: relative;
  height: 100vh;
  min-height: 500px;
  background-image: url(../../assets/images/login_bg.jpg);
  background-position: center center;
  background-repeat: no-repeat;
  background-attachment: scroll;
  background-size: 100% 100%;
  .title {
    width: 280px;
    margin: 0 auto;
    padding-top: 80px;
    font-size: 20px;
  }
  .login_form {
    width: 300px;
    height: 300px;
    padding: 0 30px;
    box-sizing: border-box;
    background-color: #fff;
    border-radius: 10px;
    position: absolute;
    top: 20%;
    left: 60%;
    .form_title {
      height: 40px;
      display: flex;
      font-size: 18px;
      align-items: center;
      justify-content: center;
      border-bottom: 1px solid #cccccc;
      .title_count {
        margin-right: 40px;
        padding-bottom: 5px;
        padding-top: 5px;
        &:nth-child(2) {
          margin-right: 0px;
        }
      }
    }
    .form_content {
      width: 100%;
      .item_captvalue {
        display: flex;
        .item_img {
          width: 100px;
          height: 36px;
          border: 1px solid #000;
          margin-left: 5px;
          margin-top: 2px;
        }
      }
      .form_foot {
        text-align: center;
        font-size: 12px;
        color: #666;
      }
      .item {
        display: flex;
        width: 100%;
        justify-content: center;
        margin-top: 20px;
      }
      .item_verification {
        display: flex;
        width: 100%;
        .el-button {
          border-left: 0 !important;
        }
        .ver_btn {
          width: 70px;
          font-size: 12px;
          display: flex;
          justify-content: center;
        }
        .ver_input {
          width: 165px;
          border-right: none;
        }
      }
      .el-form-item {
        margin-bottom: 10px;
      }
      .item_pass {
        display: flex;
        justify-content: center;
        width: 100%;
      }
      .item_btn {
        display: flex;
        justify-content: center;
        .el-button {
          width: 234px;
        }
      }
      .el-input__inner {
        width: 100%;
      }
    }
  }
}
</style>
